package mindshare;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import info.androidhive.slidingmenu.Answer;
import info.androidhive.slidingmenu.R;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnKeyListener;
import android.widget.EditText;

public class Login extends Activity{
	private Bundle b;
	SessionManager session;
	String password;
	String nickname;
	List<NameValuePair> param = new ArrayList<NameValuePair>();
	JSONParser jParser = new JSONParser();
	public List<String> contentList= new ArrayList<String>();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		
		try {
			b = getIntent().getExtras();
			Answer.nicknameAnswer=b.getString("nickname");
		} catch (Exception e) {
			Log.d("DEBUGEANDO", "NO HABIA EXTRA EN MAIN "+e.getMessage());
		}
		
		
		session = new SessionManager(getApplicationContext());
		if(session.isLoggedIn()){
			Intent intencion = new Intent(this,MainActivity.class);
			this.startActivity(intencion);
			
			
		}
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login_layout);
		
		EditText etpassword = (EditText)findViewById(R.id.password);
		etpassword.setOnKeyListener(new OnKeyListener() {
			@Override
			public boolean onKey(View v, int keyCode, KeyEvent event) {
				if (keyCode == KeyEvent.KEYCODE_ENTER) {
                    login(v);
                    return true;
                } else {
                    return false;
                }
			}
        });
	}
	
	@Override
	protected void onResume(){
		super.onResume();
		if(session.isLoggedIn()){
			mostrarMain();
		}
//		session.checkLogin();
	}
	
	@Override
	public void onStop() {
	    super.onStop();
	    finish();
	}
	
	@Override
	public void onPause() {
	    super.onPause();
	    finish();
	}
	
	class checkPassword extends LoadContent{
		String pass;
		
		public checkPassword(Context context, List<NameValuePair> param, String data) {
			super(context, param, data);
		}

		@Override
		protected String doInBackground(String... args) {
			JSONObject json = jParser.makeHttpRequest("http://mindshare.comli.com/getPassword.php", param);
			try {
				pass = json.getString("password");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			return null;
		}
		
		@Override
		protected void onPostExecute(String file_url) {
			Log.d("Password",pass);
	        pDialog.dismiss();
	        if (pass.equals(password)){
	        	session.createLoginSession(nickname);
	        	mostrarMain();
	        } else {
	        	AlertDialog.Builder builder = new AlertDialog.Builder(c);
	            builder.setTitle("Error");
	            builder.setMessage("The password/user combination is incorrect. Please try again.");
	            builder.setPositiveButton("OK",null);
	            builder.create();
	            builder.show(); 
	        }
	        
		}
		
	}
	
	public void login(View v){
		EditText etnickname = (EditText)findViewById(R.id.userName);
		EditText etpassword = (EditText)findViewById(R.id.password);
		nickname = etnickname.getText().toString();
		password = etpassword.getText().toString();
		param.clear();
		param.add(new BasicNameValuePair("nickname", nickname));
		checkPassword checkPassword= new checkPassword(this, param, "password");
		checkPassword.execute();		
	}
	
	public void mostrarMain(){
		Intent intencion = new Intent(this,MainActivity.class);
		this.startActivity(intencion);
		this.finish();
	}
	
	public void mostrarSignUp(View v){
		Intent intencion = new Intent(this,Signup.class);
		this.startActivity(intencion);
		this.finish();
	}
}
